package com.theshy.test.controller;

import com.theshy.test.dao.Student;
import com.theshy.test.service.StudentService;

import java.util.Scanner;

public abstract class BassStudentController {

    public Scanner sc = new Scanner(System.in);
    StudentService studentService = new StudentService();

    public void start() {


        WBK:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");

            String choice = sc.next();
            switch (choice) {
                case "1":
                    AddStudent();
                    break;
                case "2":
                    Delete();
                    break;
                case "3":
                    change();
                    break;
                case "4":
                    check();
                    break;
                case "5":
                    System.out.println("学生管理系统结束！");
                    break WBK;
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }
    }


    public final void AddStudent() {
        int id;
        while (true) {
            System.out.println("请输入学号:");
            id = sc.nextInt();
            boolean bo = studentService.isExist(id);
            if (bo) {
                System.out.println("id存在请重新输入");
            } else {
                break;
            }
        }

        Student s = inputStudentinfo(id);

        boolean b = studentService.Addstudent(s);

        if (b) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }

    }

    public final void Delete() {
        int id = input();

        studentService.Delete(id);

        System.out.println("学生已经删除！");

    }

    public final void change() {
        int id =input();

        Student s = inputStudentinfo(id);

        boolean b = studentService.change(s);

        if (b) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }


    }

    public final void check() {

        Student[] student = studentService.checkStudent();

        if (student == null) {
            System.out.println("暂无数据！请先添加后查看!");
            return;
        }

        System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < student.length; i++) {

            Student stu = student[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getDay());
            }
        }

    }

    public int input() {


        System.out.println("请输入学号:");
        int id;
        while (true) {

            id = sc.nextInt();
            boolean bo = studentService.isExist(id);
            if (bo) {
                break;
            } else {
                System.out.println("id不存在请重新输入");
            }
        }

        return id;
    }

    public abstract Student inputStudentinfo(int id);
}
